/**
 * @Description:
 * @author Sun Yan
 * @date $
 */

export default {
    beforeRouteEnter: function (to, from, next) {
        next()
    },
    /**
     * 删除缓存路由组件 (预想效果) -> push 前进 更新 -> goBack 后退 删除 ->  跳出 replace 非tabbar 全删除
     */
    beforeRouteLeave: function(to, from, next) {
        // let ignore = ['/article/desc', '/homepage']
        let ignore = []
        // 删除缓存组件
        if( ['goBack', 'replace'].includes(from.meta.operatingMethod) && this.$vnode.parent && !from.meta.tabbar) { // 判断是返回上一页 并且不是底部按钮
            let vnode = this.$vnode;
            let cache = vnode.parent.componentInstance.cache;
            let keys = vnode.parent.componentInstance.keys;
            let key = vnode.key == null ? vnode.componentOptions.Ctor.cid + (vnode.componentOptions.tag ? `::${vnode.componentOptions.tag}` : "") : vnode.key;
            let index = keys.indexOf(key);
            if(from.meta.operatingMethod === 'goBack') {
                // console.log(cache)
                // console.log(keys)
                // console.log(key)
                // console.log(index)
                if (cache[key]) {
                    if (keys.length) {
                        index = keys.indexOf(key);
                        if (index > -1) {
                            keys.splice(index, 1);
                        }
                    }
                    delete cache[key];
                }
            } else if(from.meta.operatingMethod === 'replace') {
                const tabs = ["/home", '/food', "/user", '/food/comestible']
                for(key in cache) {
                    if(!tabs.includes(key)) {
                        if (cache[key]) {
                            if (keys.length) {
                                index = keys.indexOf(key);
                                if (index > -1) {
                                    keys.splice(index, 1);
                                }
                            }
                            delete cache[key];
                        }
                    }
                }
            }
            // console.log(cache)
        }
        next()
    }

}